$(function () {
    //加载转出仓库
    $("[name='zkd.zcck.id']").ckcombobox();
    //加载转入仓库
    $("[name='zkd.zrck.id']").ckcombobox();

    //新增领料单明细
    $("#zkd_zkdmx_toolbar [name='xzzkdmx']").click(function () {
        var thck_id = $("#zkd_zcck_id").combobox('getValue');
        var sbgs_id = $("#zkd_sqgs_id").combotree('getValue');
        if (thck_id) {
            searchkhwindow(zkdmx_searchWindowOKbtnHandler, thck_id, sbgs_id, sbgs_id, null, null);
        } else {
            $('#zkdForm').tabs('select', "转库信息");
            $.messager.alert("提示", "请选择转出仓库!", "info");
        }
    });
    //删除领料单明细
    $("#zkd_zkdmx_toolbar [name='sczkdmx']").click(function () {
        var rows = $("#zkd_mx").edatagrid('getChecked');
        if (rows) {
            $.messager.confirm('提示', '是否确定删除转库明细?', function (r) {
                if (r) {
                    $.each(rows, function (i, val) {
                        var index = $("#zkd_mx").edatagrid('getRowIndex', val);
                        $("#zkd_mx").edatagrid('deleteRow', index);
                    });
                }
            });
        }
        $("#zkd_mx").edatagrid('resize');
    });

    var zkdId = $("[name='zkd.id']").val();
    zkdmxGrid($.webPath() + '/kcgl/zksq/zkd_zkdmx?zkdId=' + zkdId);
    //加载查询条件中的所属公司下拉框
    $("#zkd_sqgs_id").combotree({
        loadFilter:function (rows) {
            return convert(rows);
        }
    });

    //新增合同费用
    $("#zkd_zkdfy_toolbar [name='xzzkdfy']").click(function () {
        var data = {
            'sydw.id':null,
            'fylb.id':null,
            'jfsl':0,
            'dj':0,
            'je':0,
            'bz':null
        };
        $("#zkd_fy").edatagrid('appendRow', data);
    });
    //删除合同费用
    $("#zkd_zkdfy_toolbar [name='sczkdfy']").click(function () {
        var rows = $("#zkd_fy").edatagrid('getChecked');
        if (rows) {
            $.messager.confirm('提示', '是否确定删除转库费用?', function (r) {
                if (r) {
                    $.each(rows, function (i, val) {
                        var index = $("#zkd_fy").edatagrid('getRowIndex', val);
                        $("#zkd_fy").edatagrid('deleteRow', index);
                    });
                }
            });
        }
        $("#zkd_fy").edatagrid('resize');
    });

    $.post($.webPath() + '/organ/wls/wlsInuseList', function (jgdata) {
        $.post($.webPath() + '/cwgl/kmys/cyfyys/fylbcombobox', {'gsId':$("#zkd_sqgs_id").combobox('getValue')}, function (data) {
            zkdfyGrid($.webPath() + '/kcgl/zksq/zkdgl_zkfy?zkdId=' + $("[name='zkd.id']").val() || '', jgdata, data);
        })
    })

})

function zkdmxGrid(href, cdOptions, fsOptions) {
    $("#zkd_mx").edatagrid({
        url:href,
        fitColumns:false,
        singleSelect:true,
        fit:true,
        checkbox:true,
        checkOnSelect:false,
        selectOnCheck:false,
        showFooter:true,
        rownumbers:true,
        toolbar:"#zkd_zkdmx_toolbar",
        columns:[
            [
                {field:'checked', checkbox:true},
                {field:'wzgg.id', title:'物资ID', hidden:true},
                {field:'wzgg.code', title:'物资编码', width:100, align:'center'},
                {field:'wzgg.name', title:'物资名称', width:100, align:'center'},
                {field:'wzgg.ggxh', title:'规格型号', width:100, align:'center'},
                {field:'kh.id', title:'卡号ID', hidden:true},
                {field:'kh.khbm', title:'卡号', width:120, align:'center'},
                {field:'yksl', title:'原库数量', hidden:true, align:'right',
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'zkjs', editor:{type:'numberbox', options:{min:0, precision:0}}, title:'申报件数', width:100, align:'right'},
                {field:'zksl', editor:{type:'numberbox', options:{min:0, precision:4}}, title:'申报数量', width:100, align:'right',
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'bz', editor:{type:'text'}, title:'备注', width:100, align:'center'}
            ]
        ],
        onSelect:function (rowIndex, rowData) {
            //获取每个单元格
            $("#zkd_mx").edatagrid('editRow', rowIndex);
            var editors = $("#zkd_mx").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var zkslEditor = editors[1];
            zkslEditor.target.unbind('blur').bind('blur', function () {
                $(zkslEditor.target).numberbox('setValue', $(zkslEditor.target).val());
                countzkdSum(rowIndex, $(zkslEditor.target).numberbox('getValue'), 'zksl');
            });
        }
    })
}

function zkdfyGrid(href, jgOptions, fsOptions) {
    $("#zkd_fy").edatagrid({
        url:href,
        fitColumns:true,
        singleSelect:true,
        fit:true,
        checkbox:true,
        checkOnSelect:false,
        selectOnCheck:false,
        rownumbers:true,
        toolbar:"#zkd_zkdfy_toolbar",
        columns:[
            [
                {field:'checked', checkbox:true},
                {field:'sydw.id', align:'center', editor:{type:'combobox', options:{
                    data:jgOptions,
                    valueField:'id',
                    textField:'qc',
                    multiple:false,
                    panelHeight:'200'
                }
                }, formatter:function (value) {
                    for (var i = 0; i < jgOptions.length; i++) {
                        if (jgOptions[i].id == value) return jgOptions[i].qc;
                    }
                    return value;
                }, title:'收益单位', width:50, align:'center'},

                {field:'fylb.id', align:'center', editor:{type:'combobox', options:{
                    data:fsOptions,
                    valueField:'id',
                    textField:'fykmjc',
                    multiple:false,
                    panelHeight:'110'
                }
                }, formatter:function (value) {
                    for (var i = 0; i < fsOptions.length; i++) {
                        if (fsOptions[i].id == value) return fsOptions[i].fykmjc;
                    }
                    return value;
                }, title:'费用类别', width:30},
                {field:'jfsl', editor:{type:'numberbox', options:{min:0, groupSeparator:',', precision:4}}, title:'计费数量',
                    formatter:function (value) {
                        var sl = formatSl(value, ",");
                        return sl;
                    }, width:40, align:'right'},
                {field:'dj', editor:{type:'numberbox', options:{min:0, groupSeparator:',', precision:4}}, title:'单价(元)',
                    formatter:function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width:40, align:'right'},
                {field:'je', editor:{type:'numberbox', options:{min:0, groupSeparator:',', precision:4}}, title:'金额(元)',
                    formatter:function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width:50, align:'right'},
                {field:'bz', editor:{type:'text'}, title:'备注', width:50, align:'center'}
            ]
        ],
        onSelect:function (rowIndex, rowData) {
            //获取每个单元格
            $("#zkd_fy").edatagrid('editRow', rowIndex);
            var editors = $("#zkd_fy").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var jfslEditor = editors[2];
            var djEditor = editors[3];
            var jeEditor = editors[4];
            jfslEditor.target.unbind("blur").bind('blur', function () {
                $(jfslEditor.target).numberbox('setValue', $(jfslEditor.target).val());
                $(jeEditor.target).numberbox('setValue', $(jfslEditor.target).numberbox('getValue') * $(djEditor.target).numberbox('getValue'));
            });
            djEditor.target.unbind("blur").bind('blur', function () {
                $(djEditor.target).numberbox('setValue', $(djEditor.target).val());
                $(jeEditor.target).numberbox('setValue', $(jfslEditor.target).numberbox('getValue') * $(djEditor.target).numberbox('getValue'));
            });

        }
    })
}

//查询卡号物资确定按钮回调函数
function zkdmx_searchWindowOKbtnHandler(data) {
    var rowdatas = $.map(data, function (n, i) {
        return {
            'kh.id':n.id,
            'kh.khbm':n.khbm,
            'wzgg.id':n.jtwz_id,
            'wzgg.code':n.jtwz_code,
            'wzgg.name':n.jtwz_name,
            'wzgg.ggxh':n.jtwz_ggxh,
            'yksl':n.kkdsl,
            'zkjs':null,
            'zksl':n.sl,
            'bz':''
        };
    });
    $.each(rowdatas, function () {
        $("#zkd_mx").edatagrid('appendRow', this);
    });
}

//计算转库数量
function countzkdSum(index, count, type) {
    var sumzksl = 0;
    var data = $("#zkd_mx").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var zksl = parseFloat(data.rows[i]['zksl']);
        zksl = isNaN(zksl) ? 0 : zksl;
        if (type == 'zksl') {
            if (i == index)
                zksl = parseFloat(count);
        }
        sumzksl += zksl;
    }
    addFooter(sumzksl);
}

//在表底加载转库数量
function addFooter(sumzksl) {
    var footer = [
        {"wzgg.code":"合计", "zksl":formatMoney(sumzksl)}
    ];
    if (footer != null)
        $("#zkd_mx").edatagrid('reloadFooter', footer);
}

